﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Reflection;
using System.IO;
using System.Xml;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;

namespace FrozenBubble.Screens
{
    public class PhoneMainMenu : MenuScreen
    {
        private Assembly _assembly;

        private int _spacing;
        private Vector2 _topPositon;

        private InputState _oldInput;

        public PhoneMainMenu()
        {
            _assembly = Assembly.GetExecutingAssembly();

            _spacing = 65;
            _topPositon = new Vector2(92, 255);
        }

        public override void LoadContent()
        {
            BackGround = Cache.BackgroundPhoneMenu;

            Texture2D p1gameOff = Cache.MenuOnePlayer;
            Texture2D p1pgameOver = Cache.MenuOnePlayer;

            Texture2D p2gameOff = Cache.MenuTwoPlayer;
            Texture2D p2pgameOver = Cache.MenuTwoPlayer;

            Texture2D soundOff = Cache.MenuSound;
            Texture2D soundOver = Cache.MenuSound;
            Texture2D soundActiveOff = Cache.MenuSoundActive;
            Texture2D soundActiveOver = Cache.MenuSoundActive;

            Texture2D keysOff = Cache.MenuKeys;
            Texture2D keysOver = Cache.MenuKeys;

            Texture2D highscoresOff = Cache.MenuHighScores;
            Texture2D highscoresOver = Cache.MenuHighScores;

            int i = 0;

            // Start 1P Game
            MenuEntry start1pGame = new MenuEntry("Start 1P Game", new Vector2(_topPositon.X, _topPositon.Y + i * _spacing),
                p1gameOff, p1pgameOver, true);
            start1pGame.Click += new EventHandler<EventArgs>(start1pGame_Click);
            Controls.Add(start1pGame);

            // Start 2P Game
            MenuEntry start2pGame = new MenuEntry("Start 2P Game", new Vector2(_topPositon.X, _topPositon.Y + ++i * _spacing),
                p2gameOff, p2pgameOver);
            start2pGame.Click += new EventHandler<EventArgs>(start2pGame_Click);
            Controls.Add(start2pGame);

            // Sound
            CheckableMenuEntry sound = new CheckableMenuEntry("Sound", new Vector2(_topPositon.X, _topPositon.Y + ++i * _spacing),
                soundOff, soundOver, soundActiveOff, soundActiveOver);
            sound.IsChecked = ScreenManager.Storage.IsSoundEnabled;
            sound.Click += new EventHandler<EventArgs>(sound_Click);
            Controls.Add(sound);

            // Keys
            MenuEntry keys = new MenuEntry("Keys", new Vector2(_topPositon.X, _topPositon.Y + ++i * _spacing),
                keysOff, keysOver);
            keys.Click += new EventHandler<EventArgs>(keys_Click);
            Controls.Add(keys);

            // High scores
            MenuEntry highscores = new MenuEntry("High Scores", new Vector2(_topPositon.X, _topPositon.Y + ++i * _spacing),
                highscoresOff, highscoresOver);
            highscores.Click += new EventHandler<EventArgs>(highscores_Click);
            Controls.Add(highscores);
        }

        public override void HandleInput(GameTime gameTime)
        {
            base.HandleInput(gameTime);

            InputState input = Input.State;

            if (!input.IsActionActive(InputAction.End) && _oldInput.IsActionActive(InputAction.End))
            {
                Game.Exit();
            }

            _oldInput = input;
        }

        private void start1pGame_Click(object sender, EventArgs e)
        {
            IPlayerDimensions dimensions = Configuration.PlayerDimensions;

            ScreenManager.AddScreen(new BackgroundScreen());
            if (Configuration.IsRasterVisible)
                ScreenManager.AddScreen(new Raster(dimensions, 3));
            ScreenManager.AddScreen(new Player(dimensions, new LevelSet("FrozenBubble.levels")));

            ExitScreen();
        }

        private void start2pGame_Click(object sender, EventArgs e)
        {
            Guide.BeginShowMessageBox("Start 2P Game", "This is currently not available, but as soon as the Windows version is released, multiplayer will be activated between both platforms.", new string[] { "Ok" }, 0, MessageBoxIcon.None, null, null);
        }

        private void keys_Click(object sender, EventArgs e)
        {
            Guide.BeginShowMessageBox("Keys",
@"• Double-tap to wreak havoc on the dreaded multi-coloured bubbles.
• Tap on the bottom left side to turn the cannon left.  
• Tap on the bottom right side to turn the cannon right.

Sounds easy, right? You wish...", new string[] { "Ok" }, 0, MessageBoxIcon.None, null, null);
        }

        private void fullscreen_Click(object sender, EventArgs e)
        {
            Storage storage = ScreenManager.Storage;

            storage.IsFullScreen = !storage.IsFullScreen;
            storage.Save();

            GraphicsDeviceManager graphics = ScreenManager.GraphicsDeviceManager;

            graphics.IsFullScreen = storage.IsFullScreen;
            graphics.ApplyChanges();
        }

        private void sound_Click(object sender, EventArgs e)
        {
            Storage storage = ScreenManager.Storage;

            storage.IsSoundEnabled = !storage.IsSoundEnabled;
            storage.Save();
        }

        private void highscores_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            int i = 0;
            foreach (HighScore score in Storage.HighScores)
            {
                i++;
                string name = score.PlayerName;
                string scoreText = (score.HasWon) ? "WON!" : "LVL-" + string.Format("{0:00}", score.Level);

                sb.AppendLine(string.Format("{0, 2}.\t{1, -10} ({2, 6})", i, name, scoreText));
            }

            if (sb.Length == 0)
                sb.Append("No high scores yet!");

            Guide.BeginShowMessageBox("High Scores", sb.ToString(), new string[] { "Ok" }, 0, MessageBoxIcon.None, null, null);
        }
    }
}
